package com.geekstudy.week6;

import com.geekstudy.week6.tool.JdbcTool;
import org.apache.commons.dbcp2.BasicDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * http://commons.apache.org/proper/commons-dbcp/
 *
 * commons-dbcp2 示例
 * https://git-wip-us.apache.org/repos/asf?p=commons-dbcp.git;a=tree;f=doc;hb=HEAD
 *
 * https://git-wip-us.apache.org/repos/asf?p=commons-dbcp.git;a=blob;f=doc/BasicDataSourceExample.java;h=81b6e389c57fe1f4ca42af58fdcb668e3c1aaaa7;hb=HEAD
 *
 *
 * https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2/2.8.0
 *
 */
public class Week6Work11 {

    public static void main(String[] args) {

        try {
            BasicDataSource basicDataSource = new BasicDataSource();
            String url = "jdbc:mysql://127.0.0.1:3306/geek_java4";
            String username = "java4";
            String password = "java4";
            basicDataSource.setUrl(url);
            basicDataSource.setUsername(username);
            basicDataSource.setPassword(password);

            Connection connection = basicDataSource.getConnection();
            Statement statement = connection.createStatement();

            ResultSet resultSet1 = statement.executeQuery("select 1");
            resultSet1.next();
            System.out.println("resultSet1:" + resultSet1.getString(1));
            resultSet1.close();

            ResultSet resultSet2 = statement.executeQuery("select count(id) as num from week6_8 ");
            resultSet2.next();
            System.out.println("resultSet2 num:" + resultSet2.getInt("num"));
            resultSet2.close();



            JdbcTool jdbcTool = new JdbcTool();
            String tableName = "week6_11";
            jdbcTool.createTable(tableName, statement);

            int totalSize = 10000 * 1000;
            long startTime = System.currentTimeMillis();
            for(int size = 1; size <= totalSize; size++) {
                String sql = "insert into " + tableName
                        + " (id, name, remark) values ('" + size + "', 'Name" + size + "', 'Remark11_" + size + "') ";
                statement.execute(sql);
            }
            long finishTime = System.currentTimeMillis();
            long usedTime = finishTime - startTime;
            long usedSecond = usedTime / 1000;
            long usedMinute = usedTime / 60000;
            System.out.println(" record:" + totalSize + " usedTime:" + usedTime + " usedSecond:" + usedSecond + " usedMinute:" + usedMinute);


            // close connection
            statement.close();
            connection.close();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
